import Axios from "axios";
Axios.defaults.baseURL = "http://p3h5551036.zicp.vip/api";

function get(url, params) {
  return new Promise((resolve, reject) => {
    Axios.get(url, {
      params
    })
      .then(res => {
        resolve(res.data);
      })
      .catch(err => {
        if (err.response.status === 422) {
          Vue.prototype.$Message.error("params error");
          resolve(err);
        } else {
          reject(err);
        }
      });
  });
}

function post(url, formData) {
  return new Promise((resolve, reject) => {
    Axios.post(url, formData)
      .then(res => resolve(res.data))
      .catch(err => {
        if (err.response.status === 422) {
          Vue.prototype.$Message.error("params error");
          resolve(err);
        } else {
          reject(err);
        }
      });
  });
}

function put(url, formData) {
  return new Promise((resolve, reject) => {
    Axios.put(url, formData)
      .then(res => resolve(res.data))
      .catch(err => {
        if (err.response.status === 422) {
          Vue.prototype.$Message.error("params error");
          resolve(err);
        } else {
          reject(err);
        }
      });
  });
}

function del(url) {
  return new Promise((resolve, reject) => {
    Axios.delete(url)
      .then(res => resolve(res.data))
      .catch(err => {
        if (err.response.status === 422) {
          Vue.prototype.$Message.error("params error");
          resolve(err);
        } else {
          reject(err);
        }
      });
  });
}

export default {
  get,
  post,
  put,
  del
};
